import 'package:flutter/material.dart';
import 'package:flutter_ebook/pages/douban/douban_page.dart';
import 'package:flutter_ebook/pages/home/home_page.dart';
import 'package:flutter_ebook/pages/my/my_page.dart';
import 'package:flutter_ebook/pages/store/store_page.dart';
import 'package:lazy_load_indexed_stack/lazy_load_indexed_stack.dart';
import 'package:line_icons/line_icons.dart';
import 'package:salomon_bottom_bar/salomon_bottom_bar.dart';

class RootPage extends StatefulWidget {
  const RootPage({super.key});

  @override
  State<RootPage> createState() => _RootPageState();
}

class _RootPageState extends State<RootPage> {
  List rootApp = [
    {"icon": LineIcons.home, "text": "首页"},
    {"icon": LineIcons.book, "text": "豆瓣读书"},
    {"icon": LineIcons.shoppingBag, "text": "电子书城"},
    {"icon": LineIcons.user, "text": "我的"},
  ];

  int _index = 0;
  // 默认选中第一个
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        elevation: 0,
        toolbarHeight: 0, //
        backgroundColor: Theme.of(context).colorScheme.surface,
      ),
      backgroundColor: Theme.of(context).colorScheme.surface,
      bottomNavigationBar: _getBottomNavigationBar(context),
      body: LazyLoadIndexedStack(
        index: _index,
        children: [
          HomePage(),
          const DoubanPage(),
          const StorePage(),
          const MyPage(),
        ],
      ),
    );
  }

  Widget _getBottomNavigationBar(BuildContext context) {
    return SalomonBottomBar(
        onTap: (index) {
          setState(() {
            _index = index;
          });
        },
        currentIndex: _index,
        items: List.generate(rootApp.length, (index) {
          return SalomonBottomBarItem(
            selectedColor: Theme.of(context).colorScheme.onSurface,
            unselectedColor: Theme.of(context).colorScheme.inversePrimary,
            icon: Icon(rootApp[index]['icon']),
            title: Text(rootApp[index]['text']),
          );
        }));
  }
}
